<?php

$s= Form::input('s','a0','','GET');
$p= Form::input('p','0','1','GET');
$rowperpage= Config::get('num_per_page');
$order =  Form::input('order', 'a', '', 'GET');
$where='';
$datarows='';
$rowstyle ='';
$start=Tpl::offset($rowperpage);

//---- Lay theo User
if($_SESSION['a_access']<3) {
    $where .= 'AND e_author ="' . mysql_escape_string($_SESSION['a_username']) . '"';
}

//---Loc theo tim kiem
if($s!='') {
    $s = mysql_escape_string($s);
    $where .=" AND (e_title like '%$s%' OR e_author like '%$s%' OR e_content like '%$s%')";
    $s = stripslashes($s);
}

//----Tong so dong theo dieu kien
$totalRow=Db::getOneField('count(*)','entry',"e_type=4 $where");
//------Phan trang
$paging = Tpl::page($totalRow, $rowperpage, "product/list?order=$order&s=$s&p=", 1,15);

//--- Sap xep theo
$orderby=' ORDER BY ';
     switch ($order)
    {
        case 1: 	//-------------------------
            $orderby.= 'e_title';
            break;
        case 2: 	//-------------------
            $orderby.= 'e_author';
            break;
        case 3: 	//-------------------
            $orderby.= 'e_date_posted DESC';
            break;        
        case 4: 	//-------------------
            $orderby.= 'e_status';
            break;
        default:
            $orderby.= 'entryid DESC';     
}

$row=Db::getDBRows('*', 'entry', "AND e_type=4 $where$orderby LIMIT $start,$rowperpage", 0);
if(!isset($row[0]))
    $datarows = '<tr><td colspan="7">No data found</td></tr>';
$sothutu =($p-1)*$rowperpage;
foreach ($row as $rows)
{        
    $rowstyle = ($sothutu % 2 !== 0) ? 'bgcolor="#DFF4FF"' : '';
    $status = $rows['e_status']==0 ? fl('a_hide') : fl('a_show');    
    $id = $rows['entryid'];
    $e_title = $rows['e_title'];
    if(Lang::getCode() =='en'){$e_title = $rows['e_title2'];}
    $datarows.='<tr align="center" '.$rowstyle.'>
    <td>'.++$sothutu.'</td>
    <td align="left"><a class="title" href="../service?id='.$id.'" taget="_blank">'.$e_title.'</a></td>
    <td>'.$rows['e_author'].'</td>
    <td>'.$rows['e_date_posted'].'</td>    
    <td>'.$status.'</td>
    <td><a href="product/mode/edit?id='.$id.'">Edit</a> | <a onclick="return confirm(\'' . fl('deletion_confirm') . '\')" href="product/mode/edit?id='.$id.'&del=1">Delete</a></td></tr>';
}
$datarows.='<tr><td colspan="6"> &nbsp;'.$paging.'</td></tr>';

function getSortUrl($field)
{
    $urlParams = $_GET;
    $urlParams['order'] = $field;
    return '?' . http_build_query($urlParams);
}
